package one.bugu.zookeeper.rpc.framework.service.handler;

import one.bugu.zookeeper.rpc.framework.service.zookeeper.RegisterCenterProviderImpl;
import com.google.gson.Gson;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Created Date 2019/2/14
 * Time: 16:27
 * Description:
 */
public class ProcessHandler {

    private static Gson gson = new Gson();

    /**
     * RPC反射处理逻辑
     *
     * @param msgStr
     * @return
     */
    public static String process(String msgStr) throws InvocationTargetException, IllegalAccessException {
        if (RegisterCenterProviderImpl.serverBean == null || RegisterCenterProviderImpl.serverBean.isEmpty()) {
            return "Fail process your request.No definition";
        }
        String[] messages = msgStr.split(">");
        if (messages.length < 2) {
            return "Fail process your request.request illegality";
        }
        // 类
        Object targetClass = RegisterCenterProviderImpl.serverBean.get(messages[0]);
        if (targetClass == null) {
            return "Fail process your request.Server no definition";
        }
        Class clz = targetClass.getClass();
        // 方法
        Method targetMethod = null;
        // 参数
        List<Object> objects = null;
        Method[] methods = clz.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(messages[1])) {
                if (method.getParameterCount() > 0) {
                    if (messages.length == 3) {
                        String params = messages[2];
                        String[] param = params.split("&");
                        for (int i = 0; i < param.length; i++) {
                            Object o = gson.fromJson(param[i], method.getParameterTypes()[i]);
                            if (objects == null) {
                                objects = new ArrayList<>();
                            }
                            objects.add(o);
                        }
                        if (objects.size() == method.getParameterCount()) {
                            targetMethod = method;
                            break;
                        } else {
                            objects.clear();
                            objects = null;
                        }
                    }
                } else {
                    if (messages.length == 2) {
                        targetMethod = method;
                        break;
                    }
                }
            }
        }
        if (targetMethod == null) {
            return "Fail process your request.Method no definition";
        }
        Object result;
        if (objects != null && !objects.isEmpty()) {
            result = targetMethod.invoke(targetClass, objects.toArray());
        } else {
            result = targetMethod.invoke(targetClass);
        }
        return gson.toJson(result);
    }
}
